package com.service;

import java.util.List;

import com.model.AdminChannel;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class AdminChannelServiceImpl extends AbstractDateService implements AdminChannelService {

	@Override
	public boolean saveAdminChannel(List<AdminChannel> models) {
		if(models.size()>0){
			String[] sql = new String[models.size()];
			for (int i=0;i<models.size();i++) {
				AdminChannel model = models.get(i);
				model.setId(getTableId("t_adminchannel"));
				sql[i]="insert into t_adminchannel(id,adminid,channelid) values ("+model.getId()+","+model.getAdminid()+","+model.getChannelid()+")";
				
			}
			return ConnectionManager.getInstance().executeBatch(base, sql);
		}
		return true;
		
	}

	@Override
	public boolean updateAdminChannel(String adminid,List<AdminChannel> models) {
		//delete
		this.deleteAdminChannel(new AdminChannel(0, Integer.parseInt(adminid), 0));
		if(models.size()>0){
			//save
			return this.saveAdminChannel(models);
		}
		return true;
	}

	@Override
	public List<AdminChannel> getlist(AdminChannel model) {
		String sql="select id,adminid,channelid from t_adminchannel where 1=1";
		if(model.getId()>0){
			sql+=" and id="+model.getId();
		}
		if(model.getAdminid()>0){
			sql+=" and adminid="+model.getAdminid();
		}
		if(model.getChannelid()>0){
			sql+=" and channelid="+model.getChannelid();
		}
		ModelGenerator<AdminChannel> m = new ModelGenerator<AdminChannel>(AdminChannel.class);
		return m.generateList(base, sql.toString());
	}

	@Override
	public boolean deleteAdminChannel(AdminChannel model) {
		String sql="delete from t_adminchannel where 1=1";
		if(model.getId()>0){
			sql+=" and id="+model.getId();
		}
		if(model.getAdminid()>0){
			sql+=" and adminid="+model.getAdminid();
		}
		return ConnectionManager.getInstance().execute(base, sql);
	}

}
